#!/usr/bin/env python
# -*- coding: utf-8 -*-


from utils import util
from controls import mongo

"""sight_label collection has
   sightid:     (int)
   labelid:     (int)
   text:        (str)
   like:        []
   ssid:        (str)
   atime:       (str)
"""


def find_sight_label(sightid):
    label_datas = mongo.db_shitouren.sight_label.find({
        'sightid': long(sightid),
    }).sort('_id', 1)
    label = []
    for label_data in label_datas:
        tmp = {
            'labelid': label_data['labelid'],
            'text': label_data['text'],
            'like': label_data['like'],
            'ssid': label_data['ssid'],
        }
        size = len(label)
        for i in range(0, size + 1):
            if i == size:
                label.append(tmp)
                break
            if len(tmp['like']) > len(label[i]['like']):
                label.insert(i, tmp)
                break
    return label


def get_sight_label(sightid, ssid):
    label_datas = mongo.db_shitouren.sight_label.find({
        'sightid': long(sightid),
    }).sort('_id', 1)
    label = []
    for label_data in label_datas:
        is_owner = False
        is_liked = False
        if label_data['ssid'] == ssid:
            is_owner = True
        for liked_ssid in label_data['like']:
            if liked_ssid == ssid:
                is_liked = True
                break
        tmp = {
            'sightid': label_data['sightid'],
            'labelid': label_data['labelid'],
            'text': label_data['text'],
            'like': label_data['like'],
            'ssid': label_data['ssid'],
            'is_owner': is_owner,
            'is_liked': is_liked,
            'atime': str(label_data['_id'].generation_time).split(' ')[0],
        }
        size = len(label)
        for i in range(0, size + 1):
            if i == size:
                label.append(tmp)
                break
            if len(tmp['like']) > len(label[i]['like']):
                label.insert(i, tmp)
                break
    return label


def add_sight_label(sightid, labelid, text, ssid):
    new_label = {
        'sightid': long(sightid),
        'labelid': long(labelid),
        'text': text,
        'like': [],
        'ssid': ssid,
        'atime': util.datetime_now_str(),
    }
    return mongo.db_shitouren.sight_label.insert(new_label)


def del_sight_label(sightid, labelid, ssid):
    return mongo.db_shitouren.sight_label.remove(
        {
            'sightid': long(sightid),
            'labelid': long(labelid),
        },
    )


def praise_sight_label(sightid, labelid, ssid):
    label = mongo.db_shitouren.sight_label.find_one({
        'sightid': long(sightid),
        'labelid': long(labelid),
    })
    if not label:
        return None
    if label['ssid'] == ssid:
        return None
    like = label['like']
    has = False
    for i in like:
        if i == ssid:
            has = True
    if has:
        return None
    like.append(ssid)
    return mongo.db_shitouren.sight_label.update(
        {
            'sightid': long(sightid),
            'labelid': long(labelid),
        },
        {'$set': {
            'like': like
        }
        }
    )
